#!/bin/sh
DIR=`pwd`
NODE=`which node`

# get action
ACTION=$1

# help
usage() {
    echo "Usage: ./02-logic.sh {start|stop|restart}"
    exit 1;
}

get_pid() {
    if [ -f ./run/app.pid ]; then
        echo `cat ./run/app.pid`
    fi
}

# start app
start() {
    pid=`get_pid`

    if [ ! -z $pid ]; then
        echo 'server is already running'
    else
        $NODE $DIR/app.js 2>&1 &
        echo 'server is running'
    fi
}

case "$ACTION" in
    start)
        start
        ;;
    *)
        usage
        ;;
esac
